package com.dengcj.ecology.workflow;

import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * @author dengchunjie
 * @Description: 工作流工具类
 * @date 2022/1/11 11:55
 */
public class WorkflowUtil {
    /**
     * 根据工作流ID得到对应数据库表名
     * @param workflowid
     * @return
     */
    public static String getTableNameByWorkflowid(String workflowid){
        String table = "";
        String sql = "select bll.tablename from workflow_bill bll left join workflow_base bs on bll.id=bs.formid where bs.id="+workflowid;
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if(rs.next()){
            table = Util.null2String(rs.getString("tablename"));
        }
        return table;
    }

    /**
     * 根据工作流请求ID获取对应的数据库表名
     * @param requestid
     * @return
     */
    public static String getFormtableByRequestid(String requestid){
        String table = "";
        String sql = "select rb.requestid,wf.tablename from WORKFLOW_REQUESTBASE rb left join WORKFLOW_BASE wb on rb.workflowid=wb.id left join WORKFLOW_BILL wf on wf.id=wb.formid where rb.requestid="+requestid;
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if(rs.next()){
            table = Util.null2String(rs.getString("tablename"));
        }
        return table;
    }

    /**
     * 获取工作流请求当前节点类型
     * @param requestid
     * @return 0：创建，1：批准，2：提交，3：归档
     */
    public static int getReqeustCurrnetType(String requestid){
        int type = -1;
        RecordSet rs = new RecordSet();
        rs.executeQuery("select currentnodetype from workflow_requestbase where requestid=?",requestid);
        if(rs.next()){
            type = rs.getInt("currentnodetype");
        }

        return type;
    }

    /**
     * 根据requestid获取请求的创建人
     * @param requestid
     * @return
     */
    public static String getRequestCreator(String requestid){
        String creator = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery("select creater from workflow_requestbase where requestid=?",requestid);
        if(rs.next()){
            creator = Util.null2String(rs.getString("creater"));
        }

        return creator;
    }

    /**
     * 根据workflowid和节点名称获取节点id
     * @param workflowid
     * @param nodename
     * @return
     */
    public static int getNodeidByWorkflowIdAndNodename(String workflowid,String nodename){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select a.nodeid from workflow_flownode a inner join workflow_nodebase b on b.id=a.nodeid where a.workflowid=? and b.nodename=?", workflowid, nodename);
        int nodeid = -1;
        if(rs.next()){
            nodeid = rs.getInt("nodeid");
        }
        return nodeid;
    }

    /**
     * 获取流程最后操作类型  submit提交  reject退回
     * @param requestid
     * @return
     */
    public static String getRequestLastOperateType(String requestid){
        if("".equals(requestid)){
            return "";
        }

        RecordSet rs = new RecordSet();
        rs.executeQuery("select top 1 operatetype from workflow_requestoperatelog where requestid = ? order by id desc", requestid);
        if(rs.next()){
            return Util.null2String(rs.getString("operatetype"));
        }

        return "";
    }
}
